package com.jame.basics.stream;

import org.junit.Test;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

public class MyStream {
    private static List<Student> studentList2 = new ArrayList<>();
    private static Student[] students = new Student[10];

    private static List<Student> studentList = new ArrayList<>();
    private static Student student = new Student("小明", 7, 1, 1);

    static {
        studentList.add(student);
        studentList.add(student);
        studentList.add(new Student("小花", 7, 2, 0));
        studentList.add(new Student("小李", 9, 3, 0));
        studentList.add(new Student("小赵", 8, 3, 1));
        studentList.add(new Student("小王", 7, 3, 0));
        studentList.add(new Student("小亮", 6, 2, 1));
        studentList.add(new Student("小光", 6, 1, 0));
    }

    @Test
    public void test1() {
        //获取一个串行的stream流,泛型为集合中的类型
        Stream<Student> stream = studentList.stream();

        //获取一个数组流
        Stream<Student> stream1 = Arrays.stream(students);

        //通过stream的静态方法of来获取stream流 重载
        Stream<List<Student>> stream2 = Stream.of(MyStream.studentList);
        Stream<List<Student>> stream3 = Stream.of(MyStream.studentList, studentList2);

        //创建无限流
        Stream<Integer> stream4 = Stream.iterate(0, (x) -> x + 1);
        stream4.forEach(System.out::println);

        //生成
        //Stream.generate();
    }

    @Test
    public void test2() {
//        studentList.stream()
//                //过滤
//                .filter(student -> {
//                    System.out.println(student +"====");
//                    return student.getSex() == 1;
//                })
//                //限制元素数量,
//                .limit(2)
//                //终止操作
//                .forEach(System.out::println);

        Stream<Student> studentStream = studentList.stream()
                .filter((x) -> x.getAge() > 8);
        System.out.println("未执行终止操作前");
        studentStream.forEach(System.out::println);


    }

    @Test
    public void test3() {
        long count = studentList.stream()
                .skip(100)
                .count();
        System.out.println(count);
    }

    @Test
    public void test4() {
        studentList.stream()
                //排除重复
                .distinct()
                .forEach(System.out::println);
    }

    @Test
    public void test5() {
        String[] names = new String[]{"jame", "joker", "jack"};
        Arrays.stream(names)
                .map((name) -> {
                    System.out.println("");
                    return name.toUpperCase();
                })
                .forEach(System.out::println);
    }

    @Test
    public void test6() {
        boolean b = studentList.stream()
                .allMatch(student1 -> {
                    char name = '小';
                    char c = student1.getName().charAt(0);
                    System.out.println(c);
                    return c == name;
                });
        System.out.println(b);
    }

    @Test
    public void test7() {
        boolean b = studentList.stream()
                .anyMatch(student1 -> {
                    char name = '光';
                    char c = student1.getName().charAt(1);
                    System.out.println(c);
                    return c == name;
                });
        System.out.println(b);
    }

    @Test
    public void test8() {
        Integer i1 = 100;
        Integer i2 = 90;
        int compare = Integer.max(i1, i2);
        System.out.println(compare);
    }

    @Test
    public void test9() {
        IntStream intStream = studentList.stream()
                .mapToInt(student1 -> student1.getGrade());
        System.out.println(intStream.sum());
    }

    @Test
    public void test() {
        Stream<Stream<Student>> streamStream = studentList.stream()
                .map(MyStream::getStudentStream);
        streamStream.forEach((ss) -> {
            ss.forEach(System.out::println);
        });
    }

    @Test
    public void test0() {
        studentList.stream()
                .sorted((s1, s2) -> {
                    if (s1.getSex() > s2.getSex())
                        return 1;
                    return -1;
                })
                .forEach(System.out::println);
    }


    public static Stream<Student> getStudentStream(Student student) {
        List<Student> studentList = new ArrayList<>();
        studentList.add(student);
        return studentList.stream();
    }

    @Test
    public void test10() {
        Optional<Student> student = studentList.stream()
                .max((s1, s2) -> {
                    if (s1.getAge() > s2.getAge())
                        return 1;
                    return -1;
                });
        System.out.println(student.get());
    }

    @Test
    public void test11() {
        long count = studentList.stream()
                .count();
        System.out.println(count);
    }

    @Test
    public void test12() {
        Optional<Student> student = studentList.stream()
                .min((s1, s2) -> {
                    if (s1.getGrade() > s2.getGrade())
                        return 1;
                    return -1;
                });
        System.out.println(student.get());
    }

    @Test
    public void test13() {
        Student s = new Student();
        Optional<Student> reduce = studentList.stream()
                .reduce((x, y) -> {
                    if (x.getAge() < y.getAge())
                        s.setAge(y.getAge());
                    if (x.getGrade() < y.getGrade())
                        s.setGrade(y.getGrade());
                    return s;
                });
        System.out.println(reduce.get());
    }

    @Test
    public void test15() {
        List<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);

        Integer reduce = list.stream()
                .reduce(5, (x, y) -> {
                    System.out.println("x:" + x + "==y:" + y);
                    return x + y;
                });
        System.out.println(reduce);
    }

    @Test
    public void test16() {
        Double sAveragin = studentList.stream()
                .collect(Collectors.summingDouble(Student::getAge));
        System.out.println(sAveragin);
    }

    @Test
    public void test17() {
        Map<Integer, List<Student>> collect = studentList.stream()
                .collect(Collectors.groupingBy(Student::getGrade));
        Set<Integer> integers = collect.keySet();
        for (Integer integer : integers) {
            System.out.println("key:" + integer + "===value:" + collect.get(integer));
        }
    }

    @Test
    public void test18() {
        Map<Integer, Map<Integer, List<Student>>> map = studentList.stream()
                .collect(Collectors.groupingBy(
                        (x) -> x.getGrade(),
                        Collectors.groupingBy((y) -> y.getSex())
                ));
        Set<Integer> keySet = map.keySet();
        for (Integer k : keySet) {
            System.out.println("年级为:"+k);
            Map<Integer, List<Student>> map1 = map.get(k);
            Set<Integer> keySet1 = map1.keySet();
            for (Integer k1 : keySet1) {
                System.out.print("性别:"+k1);
                System.out.println(map1.get(k1));
            }
        }
    }

    @Test
    public void test20() {
        Map<Boolean, List<Student>> map = studentList.stream()
                .collect(Collectors.partitioningBy((x) -> x.getGrade() >= 2));
        Set<Boolean> booleans = map.keySet();
        for (Boolean aBoolean : booleans) {
            System.out.println("年级是否大于等于2年级:"+aBoolean);
            List<Student> studentList = map.get(aBoolean);
            for (Student student : studentList) {
                System.out.println(student);
            }
        }

    }
    @Test
    public void test21() {
        IntSummaryStatistics collect = studentList.stream()
                .collect(Collectors.summarizingInt((x)->x.getAge()));
        System.out.println("学生年龄的最大值:"+collect.getMax());
        System.out.println("学生年龄的最小值:"+collect.getMin());
        System.out.println("学生年龄的平均值:"+collect.getAverage());
        System.out.println("学生年龄的总和:"+collect.getSum());
    }
    @Test
    public void test22() {
        String collect = studentList.stream()
                .map(Student::getName)
                .collect(Collectors.joining(",","我是开头符号","我是结尾符号"));
        System.out.println(collect);
    }
}
